﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;
namespace DAO
{
    public class LichSuTruyCapDA
    {
        public static LichSuTruyCap GetByID(int maID)
        {
            LichSuTruyCap loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LichSuTruyCap_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maLS", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new LichSuTruyCap();
                loai.Ma = maID;
                loai.MaTK = Convert.ToInt32(dataReader["MaTK"]);
                loai.NgayTruyCap = Convert.ToDateTime(dataReader["NgayTruyCap"]);
                loai.TaiKhoanDangNhap = TaiKhoanDangNhapDA.GetByID(loai.MaTK);
             }
            cn.Close();
            return loai;
        }


        public static void Update(LichSuTruyCap ls)
        {
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LichSuTruyCap_Add", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTK",ls.MaTK ));
            cmd.Parameters.Add(new SqlParameter("@ngaTruyCap",ls.NgayTruyCap));
            cmd.ExecuteNonQuery();
            cn.Close();
        }



        public static List<LichSuTruyCap> GetByMaTaiKhoan(int maID)
        {
            List<LichSuTruyCap> dsLichSuTruyCap= new List<LichSuTruyCap>();
            LichSuTruyCap loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LichSuTruyCap_GetByMaTaiKhoan", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTK", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
           while (dataReader.Read())
            {
                loai = new LichSuTruyCap();
                loai.MaTK = maID;
                loai.Ma = Convert.ToInt32(dataReader["Ma"]);
                loai.NgayTruyCap = Convert.ToDateTime(dataReader["NgayTruyCap"]);
               dsLichSuTruyCap.Add(loai);
            }
            cn.Close();
            return dsLichSuTruyCap;
        }

        public static List<LichSuTruyCap> GetLastLoginByMaTaiKhoan(int maID)
        {
            List<LichSuTruyCap> dsLichSu= new List<LichSuTruyCap>();
            LichSuTruyCap loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LichSuTruyCap_GetLastLoginByMaTaiKhoan", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maTK", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                loai = new LichSuTruyCap();
                loai.MaTK = maID;
                loai.Ma = Convert.ToInt32(dataReader["Ma"]);
                loai.NgayTruyCap = Convert.ToDateTime(dataReader["NgayTruyCap"]);

                dsLichSu.Add(loai);
            }
            cn.Close();
            return dsLichSu;
        }   


    }
}
